/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/commons/json/serializer/ListSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: ListSerializer.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.json.serializer;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author elancom
 */
public final class ListSerializer implements ObjectSerializer {

	public static final ListSerializer instance = new ListSerializer();

	@Override
	public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {

		final boolean writeClassName = serializer.isEnabled(SerializerFeature.WriteClassName);

		final SerializeWriter out = serializer.getWriter();

		Type elementType = null;
		if (writeClassName) {
			if (fieldType instanceof ParameterizedType) {
				final ParameterizedType param = (ParameterizedType) fieldType;
				elementType = param.getActualTypeArguments()[0];
			}
		}

		if (object == null) {
			if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
				out.write("[]");
			} else {
				out.writeNull();
			}
			return;
		}

		final List<?> list = (List<?>) object;

		final int size = list.size();
		final int end = size - 1;

		if (end == -1) {
			out.append("[]");
			return;
		}

		final SerialContext context = serializer.getContext();
		serializer.setContext(context, object, fieldName);

		ObjectSerializer itemSerializer = null;
		try {
			if (size > 1 && out.isEnabled(SerializerFeature.PrettyFormat)) {
				out.append('[');
				serializer.incrementIndent();
				for (int i = 0; i < size; ++i) {
					if (i != 0) {
						out.append(',');
					}

					serializer.println();
					final Object item = list.get(i);

					if (item != null) {
						if (serializer.containsReference(item)) {
							serializer.writeReference(item);
						} else {
							itemSerializer = serializer.getObjectWriter(item.getClass());
							final SerialContext itemContext = new SerialContext(context, object, fieldName);
							serializer.setContext(itemContext);
							itemSerializer.write(serializer, item, i, elementType);
						}
					} else {
						serializer.getWriter().writeNull();
					}
				}
				serializer.decrementIdent();
				serializer.println();
				out.append(']');
				return;
			}

			out.append('[');
			for (int i = 0; i < end; ++i) {
				final Object item = list.get(i);

				if (item == null) {
					out.append("null,");
				} else {
					final Class<?> clazz = item.getClass();

					if (clazz == Integer.class) {
						out.writeIntAndChar(((Integer) item).intValue(), ',');
					} else if (clazz == Long.class) {
						final long val = ((Long) item).longValue();
						if (writeClassName) {
							out.writeLongAndChar(val, 'L');
							out.write(',');
						} else {
							out.writeLongAndChar(val, ',');
						}
					} else {
						final SerialContext itemContext = new SerialContext(context, object, fieldName);
						serializer.setContext(itemContext);

						if (serializer.containsReference(item)) {
							serializer.writeReference(item);
						} else {
							itemSerializer = serializer.getObjectWriter(item.getClass());
							itemSerializer.write(serializer, item, i, elementType);
						}

						out.append(',');
					}
				}
			}

			final Object item = list.get(end);

			if (item == null) {
				out.append("null]");
			} else {
				final Class<?> clazz = item.getClass();

				if (clazz == Integer.class) {
					out.writeIntAndChar(((Integer) item).intValue(), ']');
				} else if (clazz == Long.class) {
					if (writeClassName) {
						out.writeLongAndChar(((Long) item).longValue(), 'L');
						out.write(']');
					} else {
						out.writeLongAndChar(((Long) item).longValue(), ']');
					}
				} else {
					final SerialContext itemContext = new SerialContext(context, object, fieldName);
					serializer.setContext(itemContext);

					if (serializer.containsReference(item)) {
						serializer.writeReference(item);
					} else {
						itemSerializer = serializer.getObjectWriter(item.getClass());
						itemSerializer.write(serializer, item, end, elementType);
					}
					out.append(']');
				}
			}
		} finally {
			serializer.setContext(context);
		}
	}

}
